package com.lge.remoteshot.wifidirect;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.net.NetworkInfo;
import android.net.wifi.p2p.WifiP2pDevice;
import android.net.wifi.p2p.WifiP2pManager;
import android.net.wifi.p2p.WifiP2pManager.Channel;
import android.net.wifi.p2p.WifiP2pManager.PeerListListener;
import android.util.Log;

import com.lge.remoteshot.R;
import com.lge.remoteshot.camera.CameraActivity;

public class WiFiDirectBroadcastReceiver extends BroadcastReceiver {
	private WifiP2pManager manager;
	private Channel channel;
	private DeviceListFragment fragment;
	private CameraActivity activity;

	public WiFiDirectBroadcastReceiver(WifiP2pManager manager, Channel channel,
			CameraActivity activity) {
		// TODO Auto-generated constructor stub
		this.activity = activity;
		this.manager = manager;
		this.channel = channel;
		fragment = (DeviceListFragment) activity.getFragmentManager().findFragmentById(
				R.id.frag_list);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see android.content.BroadcastReceiver#onReceive(android.content.Context,
	 * android.content.Intent)
	 */
	@Override
	public void onReceive(Context context, Intent intent) {
		String action = intent.getAction();
		if (WifiP2pManager.WIFI_P2P_STATE_CHANGED_ACTION.equals(action)) {

			// UI update to indicate wifi p2p status.
			int state = intent.getIntExtra(WifiP2pManager.EXTRA_WIFI_STATE, -1);
			if (state == WifiP2pManager.WIFI_P2P_STATE_ENABLED) {
				// Wifi Direct mode is enabled
				fragment.setIsWifiP2pEnabled(true);
			} else {
				fragment.setIsWifiP2pEnabled(false);
				fragment.clearPeers();
			}
			Log.d(CameraActivity.TAG, "P2P state changed - " + state);
		} else if (WifiP2pManager.WIFI_P2P_PEERS_CHANGED_ACTION.equals(action)) {
			// request available peers from the wifi p2p manager. This is an
			// asynchronous call and the calling activity is notified with a
			// callback on PeerListListener.onPeersAvailable()
			if (manager != null) {
				manager.requestPeers(channel, (PeerListListener) fragment);
			}
			Log.d(CameraActivity.TAG, "P2P peers changed");
		} else if (WifiP2pManager.WIFI_P2P_CONNECTION_CHANGED_ACTION.equals(action)) {
			if (manager == null) {
				return;
			}
			NetworkInfo networkInfo = (NetworkInfo) intent
					.getParcelableExtra(WifiP2pManager.EXTRA_NETWORK_INFO);
			if (networkInfo.isConnected()) {

				// we are connected with the other device, request connection
				// info to find group owner IP
				manager.requestConnectionInfo(channel, fragment);
			} else {
				// It's a disconnect
				fragment.clearPeers();
				fragment.disconnect();
				if (activity.getMode() == CameraActivity.CAMERA_MODE_ORIGINAL)
					activity.stopClient();
				else
					activity.stopServer();

			}
		} else if (WifiP2pManager.WIFI_P2P_THIS_DEVICE_CHANGED_ACTION.equals(action)) {
			fragment.updateThisDevice((WifiP2pDevice) intent
					.getParcelableExtra(WifiP2pManager.EXTRA_WIFI_P2P_DEVICE));

		}
	}
}
/* end of file */
